package GraphTheoryTools;

import java.util.ArrayList;

import processing.core.PApplet;
import processing.core.PConstants;
import changceilingcolor003.*;

public class SupportFunction {
	
	
	/**
	 * Simmple graph layout system
	 * http://processingjs.nihongoresources.com/graphs
	 * (c) Mike "Pomax" Kamermans 2011
	 */

	// =============================================
//	      Some universal helper functions
	// =============================================

	// universal helper function: get the angle (in radians) for a particular dx/dy
	static float getDirection(double dx, double dy) {
	  // quadrant offsets
	  double d1 = 0.0;
	  
	  double d2 = PConstants.PI/2.0;
	  double d3 = PConstants.PI;
	  double d4 = 3.0*PConstants.PI/2.0;
	  // compute angle basd on dx and dy values
	  double angle = 0;
	  float adx = PApplet.abs((float)dx);
	  float ady = PApplet.abs((float)dy);
	  // Vertical lines are one of two angles
	  if(dx==0) { angle = (dy>=0? d2 : d4); }
	  // Horizontal lines are also one of two angles
	  else if(dy==0) { angle = (dx>=0? d1 : d3); }
	  // The rest requires trigonometry (note: two use dx/dy and two use dy/dx!)
	  else if(dx>0 && dy>0) { angle = d1 + PApplet.atan(ady/adx); }		// direction: X+, Y+
	  else if(dx<0 && dy>0) { angle = d2 + PApplet.atan(adx/ady); }		// direction: X-, Y+
	  else if(dx<0 && dy<0) { angle = d3 + PApplet.atan(ady/adx); }		// direction: X-, Y-
	  else if(dx>0 && dy<0) { angle = d4 + PApplet.atan(adx/ady); }		// direction: X+, Y-
	  // return directionality in positive radians
	  return (float)(angle + 2*PConstants.PI)%(2*PConstants.PI); }

	// universal helper function: rotate a coordinate over (0,0) by [angle] radians
	static int[] rotateCoordinate(float x, float y, float angle) {
	  int[] rc = {0,0};
	  rc[0] = (int)(x*PApplet.cos(angle) - y*PApplet.sin(angle));
	  rc[1] = (int)(x*PApplet.sin(angle) + y*PApplet.cos(angle));
	  return rc; }

	// universal helper function for Processing.js - 1.1 does not support ArrayList.addAll yet
	static void addAll(ArrayList a, ArrayList b) { for(Object o: b) { a.add(o); }}
	
}
